<?php

namespace app\mini\controller;

use app\common\controller\ApiController;
use app\common\enum\account\PayeeType;
use app\common\enum\order\OrderType;
use app\common\enum\order\PayType;
use app\common\enum\order\ReasonType;
use app\common\model\ReceiveLog;
use app\common\service\annotation\ControllerAnnotation;
use app\company\model\CompanyOrder;
use app\common\model\AccountRecord as AccountRecordModel;
use think\App;
use think\response\Json;

/**
 * @ControllerAnnotation(title="对账")
 */
class AccountRecord extends ApiController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new ReceiveLog();
    }

    public function list(): Json
    {
        if($this->scene_id == 2){
            return $this->success($this->salesman());
        }elseif ($this->scene_id == 3){
            return $this->success($this->driver());
        }
        return $this->success();
    }

    private function driver(): array
    {
        $get = $this->request->get();
        if(empty($get['pay_type'])) $get['pay_type'] = PayType::CASH;
        $where = [
            ['payee_type','=',PayeeType::DRIVER],
            ['payee_id','=',$this->uid],
            ['status','=',$this->model::STATUS_WAIT],
        ];
        $list = $this->model->field('id,store_id,company_order_id,payee_type,pay_type,payee_id,money,create_time,status')
            ->with([
                'store' => function ($query){
                    $query->field('id,name');
                }
            ])
            ->where($where)->where('pay_type',$get['pay_type'])->order('id desc')->paginate($get['limit']??15)->each(function ($item){
                $order_ids = explode(',',$item->company_order_id);
                $item->order_time = date('Y-m-d H:i:s',(new CompanyOrder())->where('id','in',$order_ids)->max('create_time'));
            })->toArray();
        $list['wait_money'] = $this->model->where($where)->where('pay_type',PayType::CASH)->sum('money');
        $list['check_money'] = $this->model->where($where)->where('pay_type',PayType::BANK)->sum('money');
        return $list;
    }

    private function salesman(): array
    {
//        $get = $this->request->get();
        $orderModel = new CompanyOrder();
        $accountRecord = new AccountRecordModel();
        $list = [
            'total' => [
                'xs' => $orderModel->where([
                    ['pid','<>',0],
                    ['salesman_id','=',$this->uid],
                    ['order_type', '=', OrderType::SALES],
                ])->sum('order_amount'),
                'tk' => $orderModel->where([
                    ['pid','<>',0],
                    ['salesman_id','=',$this->uid],
                    ['order_type', '=', OrderType::RETURN],
                ])->sum('order_amount'),
                'kd' => $orderModel->where([
                    ['pid','=',0],
                    ['salesman_id','=',$this->uid],
                ])->count(),
                'gz' => $orderModel->where([
                    ['pid','<>',0],
                    ['salesman_id','=',$this->uid],
                    ['pay_type','=',PayType::GZ],
                    ['order_type', '=', OrderType::SALES],
                ])->sum('unsay_amount'),
                'sk' => $this->model->where([
                    ['payee_type','=',PayeeType::SALESMAN],
                    ['payee_id','=',$this->uid],
                ])->sum('money'),
                'ys' => $this->model->where([
                    ['payee_type','=',PayeeType::SALESMAN],
                    ['payee_id','=',$this->uid],
                    ['reason','=',ReasonType::YC],
                ])->sum('money'),
            ],
            'xs' => [
                'dj' => $this->model->where([
                    ['payee_type','=',PayeeType::SALESMAN],
                    ['payee_id','=',$this->uid],
                    ['pay_type','=',PayType::CASH],
                    ['reason','in',[ReasonType::ORDER,ReasonType::OWE,ReasonType::ZQ]],
                    ['status','=',ReceiveLog::STATUS_WAIT],
                ])->sum('money'),
                'ds' => $this->model->where([
                    ['payee_type','=',PayeeType::SALESMAN],
                    ['payee_id','=',$this->uid],
                    ['pay_type','=',PayType::BANK],
                    ['reason','in',[ReasonType::ORDER,ReasonType::OWE,ReasonType::ZQ]],
                    ['status','=',ReceiveLog::STATUS_WAIT],
                ])->sum('money'),
                'qk' => $orderModel->where([
                    ['pid','<>',0],
                    ['salesman_id','=',$this->uid],
                    ['pay_type','=',PayType::GZ],
                    ['order_type', '=', OrderType::SALES],
                ])->sum('unsay_amount'),
                'sk' => $this->model->where([
                    ['payee_type','=',PayeeType::SALESMAN],
                    ['payee_id','=',$this->uid],
                ])->sum('money'),
            ],
            'yc' => [
                'dj' => $this->model->where([
                    ['payee_type','=',PayeeType::SALESMAN],
                    ['payee_id','=',$this->uid],
                    ['pay_type','=',PayType::CASH],
                    ['reason','=',ReasonType::YC],
                    ['status','=',ReceiveLog::STATUS_WAIT],
                ])->sum('money'),
                'ds' => $this->model->where([
                    ['payee_type','=',PayeeType::SALESMAN],
                    ['payee_id','=',$this->uid],
                    ['pay_type','=',PayType::BANK],
                    ['reason','=',ReasonType::YC],
                    ['status','=',ReceiveLog::STATUS_WAIT],
                ])->sum('money'),
                'sk' => $this->model->where([
                    ['payee_type','=',PayeeType::SALESMAN],
                    ['payee_id','=',$this->uid],
                ])->sum('money'),
            ],
            'refund' => [

            ],
        ];
        return $list;
    }
}